<template>
  <el-dialog title="删除收藏类别"
    :visible="dialogVisible"
    width="450px"
    @close="close">
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm" label-position="top">
      <el-form-item label="收藏类别内容" prop="status">
        <el-radio-group v-model="ruleForm.status">
          <el-row style="margin-bottom: 30px;">
            <el-col :span="24">
              <el-radio label="move">
                <el-select
                  v-model="type"
                  filterable
                  allow-create
                  default-first-option
                  placeholder="移动到其他类别">
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-radio>
            </el-col>
          </el-row>
          <el-row style="margin-bottom: 30px;">
            <el-col :span="24">
              <el-radio label="delete">一并删除（删除操作不可恢复！）</el-radio>
            </el-col>
          </el-row>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button  type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        status: 'move'
      },
      loading: false,
      type: '',
      typeOptions: [],
      id: ''
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  created () {
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields()
      this.type = ''
      this.$emit('update:dialogVisible', false)
    },
    open (id) {
      this.id = id
      this.getTypeList()
      this.$emit('update:dialogVisible', true)
    },
    reset(formName) {
      this.$refs[formName].resetFields()
      this.type = ''
      this.$emit('update:dialogVisible', false)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.ruleForm.status === 'move' && this.type === '') {
            this.$message.error('请选择要移动的内容')
            this.loading = false
            return false
          } else if (this.type === this.id) {
            this.$message.error('选择了当前类别！')
            this.loading = false
            return false
          }
          this.delete(this.id)
        }
      })
    },
    delete (id) { // 删除
      this.$API({
        name: 'deleteCollectType',
        paths: [id, this.ruleForm.status, this.type ? this.type : 'type'],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200 && res.data.data === 1) {
          this.$message.success(res.data.msg)
          this.close()
          this.$emit('getAll')
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      }).finally(() => {
        this.loading = false
      })
    },
    async getTypeList () { // 根据用户id查询类别列表
      const res = await this.$API({
        name: 'getCollectTypesByUid',
        paths: [this.$Cookies.get('uid')],
        requireAuth: true
      })
      let arr = []
      res.data.data.forEach(item => {
        arr.push({
          id: item.id,
          name: item.name
        })
      })
      this.typeOptions = arr
    }
  }
}
</script>

<style>
</style>